package com.celan.year2023.month07.day30;

/**
 * @author Celan
 * @description TODO
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;
        ListNode slow = head, fast = head;
        // 先快慢指针获取交点
        do {
            if (slow.next == null || fast.next == null || fast.next.next == null)
                return null;
            slow = slow.next;
            fast = fast.next.next;
        } while (slow != fast);
        ListNode p = head;
        while (p != slow) {
            p = p.next;
            slow = slow.next;
        }
        return p;
    }
}
